package com.hzit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzit.auth.entity.SecurityUser;

import com.hzit.auth.entity.User;
import com.hzit.client.UserClient;
import com.hzit.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserClient userClient;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询数据
        R<User> r = userClient.findByUsername(username);
        User user = r.getData();

        //判断
        if(user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        com.hzit.auth.entity.User curUser = new com.hzit.auth.entity.User();
        BeanUtils.copyProperties(user,curUser);

        //根据用户查询用户权限列表
        R<List<String>> rr = userClient.findPermissionListByUserId(user.getId() + "");

        SecurityUser securityUser = new SecurityUser();
        securityUser.setCurrentUserInfo(curUser);
        securityUser.setPermissionValueList(rr.getData());
        return securityUser;
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("123");
        System.out.println("encode = " + encode);
    }
}
